package logic

import (
	"context"
	"course-admin/model"
	"errors"
	"strings"

	"course-admin/userrpc/internal/svc"
	"course-admin/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 发布评论
func (l *CommentAddLogic) CommentAdd(in *userrpc.CommentAddRequest) (*userrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	if strings.Contains(in.Comment, "欺诈") || strings.Contains(in.Comment, "伪劣") {
		return nil, errors.New("拦截违规信息")
	}
	comment := model.Comment{
		UserId:   int(in.UserId),
		GoodsId:  int(in.GoodsId),
		Comments: in.Comment,
	}

	if err := comment.CommentAdd(model.DB); err != nil {
		return nil, errors.New("添加失败")
	}

	return &userrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
